package com.hgyc.mom.tool.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.context.FileServiceContext;
import com.hgyc.mom.common.util.http.HttpUtils;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.tool.entity.SopAppVersion;
import com.hgyc.mom.tool.service.SopAppVersionService;
import com.hgyc.mom.tool.vo.SopAppVersionParam;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

/**
 * sop版本管理Controller
 *
 * @author fwj
 * @since 2025-07-28
 */
@Tag(name = "sop版本管理控制器", description = "sop版本管理控制器")
@RestController
@RequestMapping("/tool/sop/version")
public class SopAppVersionController {
    @Autowired
    private SopAppVersionService sopAppVersionService;

    @Autowired
    private FileServiceContext fileServiceContext;

    @Operation(summary = "查询sop版本管理列表", description = "查询sop版本管理列表")
    @PostMapping("/pageList")
    public Result<IPage<SopAppVersion>> pageList(@Parameter(description = "查询参数") @RequestBody SopAppVersionParam sopAppVersionParam) {
        Page<SopAppVersion> page = new Page<>(sopAppVersionParam.getPage(), sopAppVersionParam.getPageSize());
        SopAppVersion sopAppVersion = new SopAppVersion();
        BeanUtils.copyProperties(sopAppVersionParam, sopAppVersion);
        QueryWrapper<SopAppVersion> wrapper = QueryWrapperUtil.build(sopAppVersion);
        IPage<SopAppVersion> list = sopAppVersionService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取sop版本管理详细信息", description = "获取sop版本管理详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "sop版本管理ID") @PathVariable("id") Long id) {
        return Result.success(sopAppVersionService.getById(id));
    }
    @Operation(summary = "新增sop版本管理", description = "新增sop版本管理")
    @PostMapping
    public Result<?> save(@Parameter(description = "sop版本管理") @RequestBody SopAppVersion sopAppVersion) {
        sopAppVersionService.saveVersion(sopAppVersion);
        return Result.success();
    }

    @Operation(summary = "修改sop版本管理", description = "修改sop版本管理")
    @PutMapping
    public Result<?> update(@Parameter(description = "sop版本管理") @RequestBody SopAppVersion sopAppVersion) {
        sopAppVersionService.updateVersion(sopAppVersion);
        return Result.success();
    }

    @Operation(summary = "删除sop版本管理", description = "删除sop版本管理")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        sopAppVersionService.deleteApp(Arrays.asList(ids));
        return Result.success();
    }

    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception
    {
        // 上传并返回新文件名称
        String filePath = fileServiceContext.uploadFile(file, "app");

        return Result.success("上传成功！",filePath);
    }

    @Operation(summary = "删除附件", description = "删除附件")
    @DeleteMapping("/file")
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("filePath") String filePath)
    {
        fileServiceContext.deleteFile(filePath);
        return Result.success();
    }

    @Operation(summary = "下载安装包", description = "下载安装包")
    @GetMapping("/download/{id}")
    public void download(@PathVariable("id") Long id, HttpServletResponse response) throws UnsupportedEncodingException {
        SopAppVersion sopAppVersion = sopAppVersionService.getById(id);
        byte[] file = fileServiceContext.downloadFile(sopAppVersion.getFilePath());
        String fileName = java.net.URLEncoder.encode(sopAppVersion.getFileName(), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        HttpUtils.writeData(response, file, "application/octet-stream");
    }

    @Operation(summary = "下载安装包", description = "下载安装包")
    @GetMapping("/download/version/{version}")
    public void downloadByVersion(@PathVariable("version") String version, HttpServletResponse response) throws UnsupportedEncodingException {
        SopAppVersion sopAppVersion = sopAppVersionService.getByVersion(version);
        byte[] file = fileServiceContext.downloadFile(sopAppVersion.getFilePath());
        String fileName = java.net.URLEncoder.encode(sopAppVersion.getFileName(), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+fileName);
        HttpUtils.writeData(response, file, "application/octet-stream");
    }

    @GetMapping("/lastVersion")
    public Result<String> lastVersion() {
        String version = sopAppVersionService.lastVersion();
        return Result.success("更新版本", version);
    }
}
